import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:series_play/comm/base/app_controller.dart';
import 'package:series_play/utils/log.dart';
import 'package:series_play/widget/app_text.dart';

import '../../generated/assets.dart';

abstract class BaseState<T extends StatefulWidget> extends State<T>
    with _SuperMethods, AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: getBackgroundColor(),
      appBar: hideAppbar()
          ? null
          : AppBar(
              backgroundColor: appbarBackgroundColor(),
              centerTitle: true,
              title:
                  getTitleWidget() ??
                  AppText.title(getTitle(), fontSize: 18, color: Colors.white),
              titleSpacing: titleSpacing(),
              actions: actions(),
              leading: GestureDetector(
                child: Icon(
                  Icons.arrow_back_ios_new_rounded,
                  color: Colors.white.withValues(alpha: 0.6),
                ),
                onTap: () {
                  Get.back();
                },
              ),
            ),
      bottomNavigationBar: bottomNavigationBar(),
      extendBodyBehindAppBar: true,
      body: showDefaultBackground()
          ? _backgroundWidget(context)
          : buildPage(context),
    );
  }

  @override
  bool get wantKeepAlive => false;
}

mixin _SuperMethods {
  AppController? _appController;

  AppController get appController =>
      _appController ??= Get.find<AppController>();

  final List<Worker> getXWorkers = [];

  emitEvent(Object e) {
    appController.emitEvent(e);
  }

  addWorker(Worker w) {
    getXWorkers.add(w);
  }

  destroy() {
    for (var e in getXWorkers) {
      e.dispose();
    }
  }

  /// 页面标题
  String getTitle() => '';

  Widget? getTitleWidget() => null;

  Color getBackgroundColor() => const Color(0xFF151418);

  Widget buildPage(BuildContext context);

  Widget? bottomNavigationBar() => null;

  bool hideAppbar() => false;

  Widget? _backgroundWidget(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(defaultBackgroundImg()),
          fit: BoxFit.fill,
        ),
      ),
      width: double.infinity,
      height: double.infinity,
      child: SafeArea(
        top: safeAreaFormTop(),
        bottom: safeAreaFormBottom(),
        child: buildPage(context),
      ),
    );
  }

  /// 默认背景图
  String defaultBackgroundImg() => Assets.imagesBgHome;

  /// 显示默认背景
  bool showDefaultBackground() => false;

  /// 导航颜色
  Color appbarBackgroundColor() => Colors.transparent;

  double? titleSpacing() => null;

  List<Widget>? actions() => null;

  bool safeAreaFormTop() => true;

  bool safeAreaFormBottom() => true;

  bool statusBarIconLight() => true;
}

/// 需要控制器的页面 简化
abstract class BaseControllerState<
  T extends StatefulWidget,
  C extends GetxController
>
    extends BaseState<T> {
  late C mController;

  @override
  void initState() {
    super.initState();
    mController = createController();
    Log.w("添加到缓存1");
    if (!isFindGetController()) {
      Log.w("添加到缓存11");
      Get.put(mController, tag: getControllerTag());
    }
  }

  bool isFindGetController() => false;

  C createController();

  String? getControllerTag() => null;

  @override
  void dispose() {
    if (!isFindGetController()) {
      Get.delete<C>(tag: getControllerTag());
    }
    super.dispose();
  }
}
